﻿import mx.rpc.http.HTTPService;
import mx.rpc.events.ResultEvent;

import mx.managers.PopUpManager;
import mx.collections. *;
import mx.controls. *;
import com.esri.ags.Map;
import com.esri.ags.geometry.Extent;
import com.uprr.app.tnm.model.Model;
import com.uprr.app.tnm.events.AppEvent;

import com.adobe.serialization.json.JSON

private function handleCreationComplete () : void 
{
	// Center the TitleWindow container
	// over the control that created it.
	PopUpManager.centerPopUp (this);
}


// Event handler for the OK button.
private function saveSnapShot () : void
{
	try 
	{
		var ex : Extent = Model.getInstance().map.extent;
		var httpRequest:HTTPService =new HTTPService();
		httpRequest.url="SetDefaultMapServlet";
		var parameters:Object=new Object();
		parameters.folder=Model.getInstance().overlayMapServiceFolder;
		parameters.name=Model.getInstance().overlayMapServiceName;
		parameters.id=Model.getInstance().overlayMapId;
		parameters.minx=ex.xmin;
		parameters.miny=ex.ymin;
		parameters.maxx=ex.xmax;
		parameters.maxy=ex.ymax;
		parameters.alpha=Model.getInstance().mixerValue;
		
		httpRequest.request=parameters;
		httpRequest.resultFormat="text";
		httpRequest.addEventListener(ResultEvent.RESULT, setDefaultMapResultHandler);
		httpRequest.addEventListener("fault", serviceFaultHandler);
		httpRequest.send();
		
		PopUpManager.removePopUp (this);
	} catch (error : Error)
	{
		var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when saving as the default map: " + error.message;
		errorEvent.dispatch ();
	}
}

/**
 * add the returned bkmk to the datagrid, server sode code shall figure out the bkmkid
 * 
 * @usage   
 * @return  
 */

private function setDefaultMapResultHandler(event : ResultEvent):void {
	// does nothing
}

// when something goes wrong for all HTTP services
private function serviceFaultHandler (event : Event) : void
{
	var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when adding bookmarks.";
		errorEvent.dispatch ();
}
